package com.gzy.blog.blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzy.blog.blog.entity.Article;
import com.gzy.blog.blog.entity.Blog;
import com.gzy.blog.blog.entity.Category;
import com.gzy.blog.blog.entity.Discuss;
import com.gzy.blog.blog.service.BlogService;
import com.gzy.blog.common.PageModel;

@Controller
@RequestMapping("/blog")
public class BlogController {

	@Autowired
	private BlogService blogService;

	@RequestMapping("/add")
	public String add(Blog blog) throws Exception {
		blogService.add(blog);
		return "redirect:/blog/listBlogPage";
	}

	@RequestMapping("/delete")
	public String delete(Blog blog) throws Exception {
		blogService.delete(blog);
		return "redirect:/blog/listBlogPage";
	}

	@RequestMapping("/deleteDiscuss/{id}")
	public String deleteDiscuss(@PathVariable Integer id) throws Exception {
		blogService.deleteDiscussById(id);
		return "redirect:/blog/listDiscussPage";
	}

	@RequestMapping("/listBlogPage")
	public String listBlogPage(Model model, @RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int pageSize) throws Exception {
		if (pageNo == 0) {
			pageNo = 1;
		}
		if (pageSize == 0) {
			pageSize = 10;
		}
		model.addAttribute("blogPage", blogService.listBlogPage(pageNo, pageSize));
		return "views/backstage/backstage-blog";
	}

	@RequestMapping("/update")
	public String update(Blog blog) throws Exception {
		blogService.update(blog);
		return "redirect:/blog/listBlogPage";
	}

	@RequestMapping("/getBlog")
	public String getBlog(Integer userId, Model model) throws Exception {
		Blog blog = blogService.getBlogByUserId(userId);
		PageModel<Article> articlePage = blogService.getArticlePageByUserId(userId);
		List<Article> articleList = blogService.listWellAticleSelf(userId);
		List<Article> categoryList = blogService.listArticleCategory(userId);
		int sum = blogService.getSun(categoryList);

		model.addAttribute("blog", blog);
		model.addAttribute("sum", sum);
		model.addAttribute("articlePage", articlePage);
		model.addAttribute("articleList", articleList);
		model.addAttribute("categoryList", categoryList);
		return "views/sys/private";
	}

	@RequestMapping("/get")
	@ResponseBody
	public Blog get(Integer id) throws Exception {
		Blog blog = blogService.getBlogById(id);
		return blog;
	}

	@RequestMapping("/getSingleArticle")
	@ResponseBody
	public Article getSingleArticle(Integer id) throws Exception {
		Article article = blogService.getSingleArticle(id);
		return article;
	}

	@RequestMapping("/getArticle")
	public String getArticle(Integer id, Model model) throws Exception {
		Blog blog = blogService.getBlogByArticleId(id);
		Article article = blogService.getArticleById(id);
		List<Discuss> discussList = blogService.getDiscussListByArticleId(id);
		List<Article> articleList = blogService.listWellAticleSelf(article.getUserId());
		List<Article> categoryList = blogService.listArticleCategory(article.getUserId());
		// ri
		int sum = blogService.getSun(categoryList);

		model.addAttribute("sum", sum);
		model.addAttribute("blog", blog);
		model.addAttribute("article", article);
		model.addAttribute("discussList", discussList);
		model.addAttribute("articleList", articleList);
		model.addAttribute("categoryList", categoryList);
		return "views/sys/particulars";
	}

	@RequestMapping("/initMain")
	public String initMain(Model model) throws Exception {
		PageModel<Article> articlePage = blogService.pageArticle(1, 10);
		model.addAttribute("articlePage", articlePage);

		List<Article> articleList = blogService.listWellAticle();
		model.addAttribute("articleList", articleList);
		return "main";
	}

	@RequestMapping("/listDiscussPage")
	public String listDiscussPage(Model model, @RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int pageSize) throws Exception {
		PageModel<Discuss> discussPage = blogService.pageDiscuss(pageNo, pageSize);
		model.addAttribute("discussPage", discussPage);
		return "views/backstage/backstage-discuss";
	}

	@RequestMapping("/listCategoryPage")
	public String listCategoryPage(Model model, @RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int pageSize) throws Exception {

		PageModel<Category> categoryPage = blogService.pageCategory(pageNo, pageSize);
		model.addAttribute("categoryPage", categoryPage);
		return "views/backstage/backstage-category";
	}

	@RequestMapping("/addCategory")
	public String addCategory(Category category) throws Exception {
		blogService.addCategory(category);
		return "redirect:/blog/listCategoryPage";
	}

	@RequestMapping("/deleteCategory")
	public String deleteCategory(Integer id) throws Exception {
		blogService.deleteCategory(id);
		return "redirect:/blog/listCategoryPage";
	}

	@RequestMapping("/addArticle")
	public String addArticle(Article article) throws Exception {
		blogService.addArticle(article);
		return "redirect:/blog/listArticlePage";
	}

	@RequestMapping("/deleteArticle")
	public String deleteArticle(Integer id) throws Exception {
		blogService.deleteArticle(id);
		return "redirect:/blog/listArticlePage";
	}

	@RequestMapping("/listArticlePage")
	public String listArticlePage(Model model, @RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "10") int pageSize) throws Exception {
		PageModel<Article> articlePage = blogService.pageArticle(pageNo, pageSize);
		model.addAttribute("articlePage", articlePage);

		return "views/backstage/backstage-article";
	}

	@RequestMapping("/listBlogWithUser")
	@ResponseBody
	public PageModel<Blog> listBlogWithUser(int pageNumber, int pageSize) throws Exception {
		int pageNo = pageNumber;
		if (pageNumber == 0) {
			pageNo = 1;
		}
		if (pageSize == 0) {
			pageSize = 10;
		}
		PageModel<Blog> blogPage = blogService.listBlogWithUser(pageNo, pageSize);
		return blogPage;
	}

	@RequestMapping("/pageArticle")
	@ResponseBody
	public PageModel<Article> listBlogWithUser(int pageNo) throws Exception {
		if (pageNo == 0) {
			pageNo = 1;
		}
		PageModel<Article> articlePage = blogService.pageArticle(pageNo, 5);
		return articlePage;
	}

	@RequestMapping("/addDiscuss")
	@ResponseBody
	public String addDiscuss(Discuss discuss) throws Exception {
		blogService.addDiscuss(discuss);
		return "success";
	}

}
